import { defineStore } from 'pinia'
import type { MeetingRecord } from '../types/MeetingRecord'

/**
 * 会议记录存储模块
 * 负责管理各类会议记录的全局状态
 */
export const useMeetingRecordStore = defineStore('meetingRecord', {
    // 持久化配置
    persist: {
        storage: localStorage,
        paths: ['meetingRecords', 'currentRecord']
    },
    state: () => ({
        // 所有会议记录
        meetingRecords: [] as MeetingRecord[],
        // 当前选中的记录
        currentRecord: null as MeetingRecord | null,
        // 加载状态
        loading: false,
        // 会议类型筛选
        meetingTypeFilter: null as number | null
    }),
    actions: {
        /**
         * 获取所有会议记录
         */
        async fetchAllRecords() {
            this.loading = true
            try {
                // this.meetingRecords = await meetingApi.getAllRecords()
            } finally {
                this.loading = false
            }
        },

        /**
         * 创建会议记录
         * @param newRecord - 新会议记录对象
         */
        async createRecord(newRecord: Omit<MeetingRecord, 'recordId'>) {
            this.loading = true
            try {
                // const created = await meetingApi.createRecord(newRecord)
                // this.meetingRecords.unshift(created)
            } finally {
                this.loading = false
            }
        },

        /**
         * 设置会议类型筛选
         * @param type - 会议类型(1-班会，2-组会，3-学业指导)
         */
        setMeetingTypeFilter(type: number | null) {
            this.meetingTypeFilter = type
        }
    },
    getters: {
        /**
         * 获取筛选后的会议记录
         */
        filteredRecords(state): MeetingRecord[] {
            let records = [...state.meetingRecords]
            if (state.meetingTypeFilter) {
                records = records.filter(r => r.meetingType === state.meetingTypeFilter)
            }
            return records.sort((a, b) => new Date(b.meetingTime).getTime() - new Date(a.meetingTime).getTime())
        },

        /**
         * 获取最近的会议记录（最近一个月）
         */
        recentRecords(state): MeetingRecord[] {
            const oneMonthAgo = new Date()
            oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1)
            return state.meetingRecords
                .filter(r => new Date(r.meetingTime) > oneMonthAgo)
                .slice(0, 5)
        }
    }
})
